/**
 *	bbx 
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');

	//页面跳转管理类
	var WindowTools = require('WindowTools_Core');
	var orderService = require('Order_Service');

	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			console.log("init");
			mui.init({
				statusBarBackground: '#f7f7f7'
			});
			WindowTools.preloadTemplate();
			init();
		});
	}

	function init() {
		orderService.orderList(function(data) {
			debugger;
			var lists = data.List;
			var fragment = document.createDocumentFragment();
			for(var i = 0; i < lists.length; i++) {
				var item = lists[i];
				var li;
				li = document.createElement('li');
				li.className = 'mui-table-view-cell';
				li.innerHTML += '<a href="javascript:;">' +
					'<div class="mui-media-body">' +
					'<span class="mui-left">' + item.Status + '</span>' +
					'<span class="mui-pull-right" style="font-size: 10px;color: lightgray;">' + item.Code + '</p>' +
					'</div><div style="height: 1px;background: lightgray;"></div><div>' +
					'<p>学员:<span>' + item.StudentName + '</span></p>' +
					'<p>详情:<span>' + item.GoodName + '</span></p>' +
					'<p>时间:<span>' + item.CreateTime + '</span></p>' +
					'</div><div style="height: 1px;background: lightgray;"></div>'
				if(item.Status == 'Waiting') {
					li.innerHTML += '<button type="button" class="mui-btn mui-btn-blue pay" code="'+item.Code+'">支付</button>';
				}
				li.innerHTML += '<div class="mui-pull-right">' +
					'<p style="color: black;">订单金额:&nbsp;<span style="color: red;">' + item.OrderFee + '元</span></p></div></a>';
				fragment.appendChild(li);
			}
			var dataContainer = document.getElementById('order_list');
			dataContainer.appendChild(fragment);
			initListeners();
		});
	}
	
	function initListeners() {
		mui(".mui-table-view").on("tap", ".pay", function(){
			var elem = this;
			mui.confirm('确认要支付吗？', 'Tips', ['确认', '取消'], function(e) {
				if (e.index == 0) {
					var code = elem.getAttribute('code');
					orderService.pay(code,function(){
						mui.toast('success', {
							duration: 'short',
							type: 'div'
						});
						WindowTools.reload();
					});
				} else {
					
				}
			});
		});
	}

	function initLogin() {
		//检查 "登录状态/锁屏状态" 结束
		var loginButton = document.getElementById('login'); //登录按钮
		var accountBox = document.getElementById('account'); //用户名
		var passwordBox = document.getElementById('password'); //密码
		var autoLoginButton = document.getElementById("autoLogin"); //自动登录
		var regButton = document.getElementById('reg'); //注册
		var forgetButton = document.getElementById('forgetPassword'); //忘记密码
		loginButton.addEventListener('tap', function(event) {
			var loginInfo = {
				UserAccount: accountBox.value,
				UserPassword: passwordBox.value,
				UserType: 3
			};
			console.log("login");
			accountService.login(loginInfo, function(res) {
				//				debugger;
				var style = {};
				WindowTools.openWinWithTemplate(null, "../index.html", null, style);
			});

		});

		regButton.addEventListener('tap', function(event) {
			var style = {};
			WindowTools.openWinWithTemplate(null, "./reg.html", null, style);
		}, false);
		forgetButton.addEventListener('tap', function(event) {
			var style = {};
			WindowTools.openWinWithTemplate(null, "./forget_password.html", null, style);
		}, false);
	}
});